<template>
  <div class="login-account">
    <el-form :rules="rules" :model="account" ref="formRef">
      <el-form-item label-width="60px" label="账号" prop="name">
        <el-input v-model="account.name"></el-input>
      </el-form-item>

      <el-form-item label-width="60px" label="密码" prop="password">
        <el-input v-model="account.password" show-password></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from 'vue'
import { ElForm, ElFormItem, ElInput } from 'element-plus'
import { rules } from '../config/account-config'
import localCache from '@/utils/cache'
import { useStore } from 'vuex'
import { ISpringSecurityLoginUserModel } from 'src/serivce/login/types'

export default defineComponent({
  components: { ElForm, ElFormItem, ElInput },

  setup() {
    const store = useStore()

    const account = reactive({
      name: localCache.getCache('name') ?? '',
      password: localCache.getCache('password') ?? ''
    })

    const formRef = ref<InstanceType<typeof ElForm>>()

    const doLogin = (isKeepPassword: boolean) => {
      formRef.value?.validate((valid) => {
        if (valid) {
          console.log('记住密码：', valid)
          if (isKeepPassword) {
            console.log('开始保存密码')
            localCache.setCache('name', account.name)
            localCache.setCache('password', account.password)
          }
          // 这里执行登录逻辑
          console.log('账号登录验证：', valid)
        }
      })

      // 调用 vuex 模块 login 中的 action.accountLoginAction
      // const logModel: ISpringSecurityLoginUserModel = { 'username': account.name, 'password': account.password }
      const logModel: ISpringSecurityLoginUserModel = { 'username': account.name, 'password': account.password }
      store.dispatch('login/accountLoginAction', { ...logModel })
    }

    return { account, rules, doLogin, formRef }
  }
})
</script>
<style scoped></style>
